/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.service;

import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.service.CrudService;
import cn.mcsys.myadmin.common.utils.Constant;
import cn.mcsys.myadmin.common.utils.MathUtils;
import cn.mcsys.myadmin.modules.air.dao.AirCarDao;
import cn.mcsys.myadmin.modules.share.dao.AirOutsideCarDao;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirRealtimeMessage;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.share.dto.AirCarDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 车辆管理Service
 * @author leopold
 * @version 2018-08-13
 */
@Service
@Transactional(readOnly = true)
public class AirCarService extends CrudService<AirCarDao, AirCar> {

	@Autowired
	private AirRidePointService airRidePointService;

	@Autowired
	private AirRealtimeMessageService airRealtimeMessageService;

	@Autowired
	private AirOutsideCarDao airOutsideCarDao;

	public AirCar get(Integer id) {
		return super.get(id);
	}

	/**
	 * 获取所有字段的属性(含映射关系的)
	 * @param id
	 * @return
	 */
	public AirCar getAll(Integer id){
		return dao.getAll(id);
	}

	public List<AirCar> findList(AirCar airCar) {
		return super.findList(airCar);
	}
	
	public Page<AirCar> findPage(Page<AirCar> page, AirCar airCar) {
		if(airCar.getAirRidePoint() != null && airCar.getAirRidePoint().getId() == null){
			airCar.setAirRidePoint(null);
		}
		return super.findPage(page, airCar);
	}

	/**
	 * 如果车辆没有保存乘车点,就只更新基本数据
	 * 保存调度操作记录
	 * @param airCar
	 */
	@Transactional
	public void save(AirCar airCar) {
		if(airCar.getAirRidePoint() != null){
			Date date = new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String format1 = format.format(date);
			System.out.println("当前时间："+format1);
			airCar.setModificationDate(date);
			AirRidePoint airRidePoint = airRidePointService.get(airCar.getAirRidePoint().getId());
			String note = "";
			if(airCar.getIsNewRecord()){

				dao.insert(airCar);
				note = airRidePoint.getName()+Constant.ADDCAR+airCar.getCarNumber();
				airRealtimeMessageService.setProperty(airRidePoint,AirRealtimeMessage.SCHEDULING,note);
			}else{
				AirRidePoint oldPoint = dao.getAll(airCar.getId()).getAirRidePoint();
				if (airCar.getDriver() != null){
					oldPoint.setDriverSum(MathUtils.reduceOne(oldPoint.getDriverSum()));
				}
				oldPoint.setBusSum(MathUtils.reduceOne(oldPoint.getBusSum()));
				airRidePointService.updateTemplate(oldPoint);
				dao.update(airCar);
				note = oldPoint.getName()+Constant.arror+airRidePoint.getName()+Constant.MOVECAR+airCar.getCarNumber();
				airRealtimeMessageService.setProperty(airRidePoint,AirRealtimeMessage.SCHEDULING,note);
			}
//			更改车辆数和工作人员数
			airRidePoint.setBusSum(MathUtils.addOne(airRidePoint.getBusSum()));
			if (airCar.getDriver() != null){
				airRidePoint.setDriverSum(MathUtils.addOne(airRidePoint.getDriverSum()));
			}

			airRidePointService.updateTemplate(airRidePoint);
		}else{
			super.save(airCar);
		}
	}

	@Transactional(readOnly = false)
	public void delete(AirCar airCar) {
		super.delete(airCar);
	}

	/**
	 * 根据乘车站点查询所有对应的车辆
	 * @param pointId
	 * @return
	 */
    public List<AirCar> selectByPointId(Integer pointId) {
    	return dao.selectByPointId(pointId);
    }

	/**
	 * 根据多个id查询对应的车辆
	 * @param ids
	 * @return
	 */
	public List<AirCar> selectByIds(Long[] ids) {
		return dao.selectByIds(ids);
    }


	/**
	 * 更新多个车辆的乘车点
	 * @param carIds
	 * @param pointId
	 */
	@Transactional(readOnly = false)
	public void update4pointId(Long[] carIds, Integer pointId) {
		AirRidePoint airRidePoint = airRidePointService.get(pointId);
		dao.update4pointId(carIds,pointId,airRidePoint.getName());
	}

	/**
	 * 通过乘车点查询对应的车辆
	 * @param pointId
	 * @return
	 */
	public List<AirCar> getPointCars(Long pointId) {
		return  dao.getPointCars(pointId);
	}

    /**
     * 查询所有的应急停车场的车辆
     * @return
     */
    public List<AirCar> getEmergencyCar(){
        List<AirRidePoint> points = airRidePointService.findAllList(new AirRidePoint());
        for (AirRidePoint point : points) {
            if (point.getAirParkingLot() == null) {
                List<AirCar> cars = dao.selectByPointId(point.getId());
                return cars;
            }
        }
        return null;
    }

	public AirCar selectByCarNumber(String vehicleNo) {
		return dao.selectByCarNumber(vehicleNo);
	}

	@Transactional(readOnly = false)
    public void updateCarStatus(Long airCarId, int status) {
    	dao.updateCarStatus(airCarId,status);
    }

	public List<AirCarDTO> selectTrafficList() {
		List<AirCar> cars = dao.trafficList();
		List<AirCarDTO> dtos = new ArrayList<>();
		for (AirCar car : cars) {
			AirCarDTO airCarDTO = new AirCarDTO();
			BeanUtils.copyProperties(car,airCarDTO);
			airCarDTO.setAirCarType(airCarDTO.NOW_CITY);
			dtos.add(airCarDTO);
		}
//		跨市
		List<AirCar> cars1 = airOutsideCarDao.outsideTrafficList();
		for (AirCar airCar : cars1) {
			AirCarDTO airCarDTO = new AirCarDTO();
			BeanUtils.copyProperties(airCar,airCarDTO);
			airCarDTO.setAirCarType(airCarDTO.OUT_SIDE_CITY);
			dtos.add(airCarDTO);
		}
		return dtos;
	}

	public List<AirCar> selectLikeByCarNumber(Integer pointId, String carNumber) {
		return dao.selectLikeByCarNumber(pointId,carNumber);
	}

    public List<AirCar> getCarByStatusAndAirRidePointId(Integer status, Integer airRidePointId) {
    	return dao.getCarByStatusAndAirRidePointId(status,airRidePointId);
    }

	@Transactional(readOnly = false)
	public void updateIsOnLine(Integer id, Integer isOnline) {
    	dao.updateIsOnLine(id,isOnline);
	}


	public List<AirCar> getAllPointsCar() {
    	return dao.getAllAirCar();
	}

    public AirCar getDetailByCarNumber(String carNumber) {
    	return dao.getDetailByCarNumber(carNumber);
    }

	public List<AirCar> selectZHAllCarList() {
		return dao.selectZHAllCarList();
	}

    public List<AirCar> selectAllCars() {
    	return dao.selectAllAirCar();
    }

	public List<AirCar> getAllCarsHavingGPS() {
    	return dao.getAllCarsHavingGPS();
	}

	@Transactional(readOnly = false)
    public void clearCarStatus(Integer id) {
    	dao.clearCarStatus(id);
    }
}